﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Infra_Common.redis
{
	public static class KeyGenerator
	{
		//创建一个缓存Key的生成器，以Attribute中的CacheKeyPrefix作为前缀，之后可以扩展批量删除的功能。被拦截方法的方法名和入参也同样作为key的一部分，保证Key值不重复
		public static string GetCacheKey(MethodInfo methodInfo, object[] args, string prefix)
		{
			StringBuilder cacheKey = new StringBuilder();
			cacheKey.Append($"{prefix}_");
			cacheKey.Append(methodInfo.DeclaringType.Name).Append($"_{methodInfo.Name}");
			foreach (var item in args)
			{
				cacheKey.Append($"_{item}");
			}
			return cacheKey.ToString();
		}

		public static string GetCacheKeyPrefix(MethodInfo methodInfo, string prefix)
		{
			StringBuilder cacheKey = new StringBuilder();
			cacheKey.Append(prefix);
			cacheKey.Append($"_{methodInfo.DeclaringType.Name}").Append($"_{methodInfo.Name}");
			return cacheKey.ToString();
		}
	}
}
